
#Author: Luke Keele
#Matching Analysis for Black Mayoral Turnout in LA
#Date: 6/5/2013

#Load Libraries
library(foreign)
library(rbounds)
library(mipmatch)
library(xtable)

rm(list=ls())

setwd("~/Documents/Black Mayors/Replication File/Match Functions")

source("errorhandling.R")
source("constraintmatrix2.R")
source("problemparameters2.R")
source("subsetmatch2.R")

###################################################################
# Match Two - Runoff Elections
###################################################################
setwd("~/Documents/Black Mayors/Replication File/Data")

vturn <- read.dta("match-runoff.dta")
vturn <- vturn[,c("black", "pop90", "blackpop_pct1990", "year", "black_turnout", 
                  "fipscode", "city_name", "year", "day", "month", "no_cand", 
                  "college_pct", "unemp", "income", "poverty", "hs_pct", 
                  "home", "new_id") ]

vturn = vturn[order(vturn$black, decreasing = TRUE), ]
head(vturn)
tail(vturn)
n <- nrow(vturn)
vturn$id <- seq(1:n)

# Treatment indicator 
	t_ind = vturn$black

# Matrix of covariates
	X_mat = cbind(vturn$pop90, vturn$blackpop_pct1990,  
	              vturn$college_pct, vturn$hs_pct, 
	              vturn$unemp, vturn$income, 
				  vturn$poverty, vturn$home, 
				  vturn$no_cand)					
					
# Prematch Balance		  	  
KS_out_pre <-  MatchBalance(black~ pop90 + blackpop_pct1990 + college_pct + hs_pct + 
							   unemp + income + poverty + home + no_cand, 
							   ks=TRUE, nboots=1000, data=vturn)

out_pre <- matrix(NA, 9,3)

for (i in 1:9){
	out_pre[i,1] <- unlist((KS_out_pre$BeforeMatching)[[i]])[3]
  	out_pre[i,2] <- unlist((KS_out_pre$BeforeMatching)[[i]])[4]
  	out_pre[i,3] <- unlist((KS_out_pre$BeforeMatching)[[i]])[11]

}
out_pre

# Convert Proportions to Percentages
out_pre[2,1] <-  out_pre[2,1]*100
out_pre[3,1] <-  out_pre[3,1]*100
out_pre[4,1] <-  out_pre[4,1]*100
out_pre[5,1] <-  out_pre[5,1]*100
out_pre[7,1] <-  out_pre[7,1]*100
out_pre[8,1] <-  out_pre[8,1]*100

out_pre[2,2] <-  out_pre[2,2]*100
out_pre[3,2] <-  out_pre[3,2]*100
out_pre[4,2] <-  out_pre[4,2]*100
out_pre[5,2] <-  out_pre[5,2]*100
out_pre[7,2] <-  out_pre[7,2]*100
out_pre[8,2] <-  out_pre[8,2]*100

out_pre

#Create Rank-based Mahalanobis Distance Matrix
dist_mat = distmat(t_ind, X_mat)

# Number of matches
	n_matches=1
	
# Moment covariates
mom_covs = cbind(vturn$blackpop_pct1990, vturn$pop90,   
			     vturn$college_pct, vturn$hs_pct, 
			     vturn$unemp, vturn$income, vturn$poverty, 
			     vturn$home) 
			     
mom_weights = NULL
mom_tols = c(.02, 5000, .05, .05, .05, 5000, .05, .05)
 	
# Kolmogorov-Smirnov covariates 
ks_covs = cbind(vturn$blackpop_pct1990, vturn$pop90) 
ks_n_grid = 10
ks_weights = NULL
ks_tols = c(.02, 5000)
 
# Covariates for near-exact matching, fine and near-fine balance  Sexact_covs = NULL
exact_covs = NULL
near_exact_covs = cbind(vturn$year)
near_exact_devs = 8
fine_covs = NULL
near_fine_covs = NULL
near_fine_devs = NULL
 	
 	
# Whether specific controls need to be used
use_controls = NULL
 	
# Enforce all the constraints
enforce_constraints = FALSE
	
# Whether all the treated units need to be used
	subset_weight = 1
	
out.2 = subsetmatch(dist_mat, t_ind, n_matches, 
					  mom_covs, mom_weights, mom_tols,
					  exact_covs, 
					  near_exact_covs, near_exact_devs, 
					  fine_covs, 
					  near_fine_covs, near_fine_devs, 
					  subset_weight,
					  use_controls,
					  enforce_constraints)

#Compare Number of Treated Obs Before and After Matching
table(t_ind)
c(length(out.2$t_id), length(out.2$c_id))
	
data.match.runoff <- vturn[c(out.2$t_id, out.2$c_id),]
data.match.runoff$pair.id <- out.2$group_id
data.match.runoff$city_name

# Describe covariate balance 	
KS_out_post <-  MatchBalance(black~ pop90 + blackpop_pct1990 + college_pct + hs_pct + 
							   unemp + income + poverty + home + no_cand, 
							   ks=TRUE, nboots=1000, data=data.match.runoff)

out_post <- matrix(NA, 9,3)

for (i in 1:9){
	out_post[i,1] <- unlist((KS_out_post$BeforeMatching)[[i]])[3]
  	out_post[i,2] <- unlist((KS_out_post$BeforeMatching)[[i]])[4]
  	out_post[i,3] <- unlist((KS_out_post$BeforeMatching)[[i]])[11]

}

out_post
# Convert Proportions to Percentages
out_post[2,1] <-  out_post[2,1]*100
out_post[3,1] <-  out_post[3,1]*100
out_post[4,1] <-  out_post[4,1]*100
out_post[5,1] <-  out_post[5,1]*100
out_post[7,1] <-  out_post[7,1]*100
out_post[8,1] <-  out_post[8,1]*100

out_post[2,2] <-  out_post[2,2]*100
out_post[3,2] <-  out_post[3,2]*100
out_post[4,2] <-  out_post[4,2]*100
out_post[5,2] <-  out_post[5,2]*100
out_post[7,2] <-  out_post[7,2]*100
out_post[8,2] <-  out_post[8,2]*100

out_post
names <- c("Municipal Population", "African American (%)", "College Degree (%)", 
			"High School (%)", "Unemployed (%)", "Median Income", "Below Poverty Line (%)",
			"Home Rule (0/1)", "# First Round Candidates")
out <- cbind(out_pre, out_post)			
rownames(out) <- names
out
xtable(out, digits=c(1,1,1,2,1,1,2))

t_id = out.2$t_id	
c_id = out.2$c_id
xtable(finetab(vturn$year, t_id, c_id))

setwd("~/Documents/Black Mayors/Replication File/Matches")
save(data.match.runoff, file="MatchRunOff.RData")

# Balance Stats for Data Not In Match
data.notmatch <- vturn[c(-out.2$t_id, -out.2$c_id),]

MatchBalance(black~ pop90 + blackpop_pct1990 + college_pct + hs_pct + 
							   unemp + income + poverty + home + no_cand, 
							   ks=TRUE, nboots=1000, data=data.match.runoff)

KS_notmatch <-  MatchBalance(black~ pop90 + blackpop_pct1990 + college_pct + hs_pct + 
							   unemp + income + poverty + home + no_cand, 
							   ks=TRUE, nboots=1000, data=data.notmatch)
out_notmatch <- matrix(NA, 9, 2)

for (i in 1:9){
	out_notmatch[i,1] <- unlist((KS_notmatch $BeforeMatching)[[i]])[3]
  	out_notmatch[i,2] <- unlist((KS_notmatch $BeforeMatching)[[i]])[4]  	
}

out_notmatch
out_notmatch[2,1] <-  out_notmatch[2,1]*100
out_notmatch[3,1] <-  out_notmatch[3,1]*100
out_notmatch[4,1] <-  out_notmatch[4,1]*100
out_notmatch[5,1] <-  out_notmatch[5,1]*100
out_notmatch[7,1] <-  out_notmatch[7,1]*100
out_notmatch[8,1] <-  out_notmatch[8,1]*100

out_notmatch[2,2] <-  out_notmatch[2,2]*100
out_notmatch[3,2] <-  out_notmatch[3,2]*100
out_notmatch[4,2] <-  out_notmatch[4,2]*100
out_notmatch[5,2] <-  out_notmatch[5,2]*100
out_notmatch[7,2] <-  out_notmatch[7,2]*100
out_notmatch[8,2] <-  out_notmatch[8,2]*100
	
tabout <- cbind(out_notmatch, out_post[,1:2])

rownames(tabout) <- names
print(xtable(tabout, digits=1), include.rownames=TRUE)


###################################################################
# Match Three - Runoff Elections W/1 Af-Am in General
###################################################################
rm(list=ls())

setwd("~/Documents/Black Mayors/Replication File/Match Functions")

source("errorhandling.R")
source("constraintmatrix2.R")
source("problemparameters2.R")
source("subsetmatch2.R")


setwd("~/Documents/BlackCandidates/Data/Louisiana/Final")
vturn <- read.dta("match-runoff.dta")
vturn <- subset(vturn, black_gen==1)
vturn <- vturn[,c("black", "pop90", "blackpop_pct1990", "year", "black_turnout", 
                  "fipscode", "city_name", "year", "day", "month", "no_cand", 
                  "college_pct", "unemp", "income", "poverty", "hs_pct", 
                  "home", "new_id") ]

vturn = vturn[order(vturn$black, decreasing = TRUE), ]

head(vturn)
tail(vturn)
n <- nrow(vturn)
vturn$id <- seq(1:n)

# Treatment indicator 
	t_ind = vturn$black

# Matrix of covariates
	X_mat = cbind(vturn$pop90, vturn$blackpop_pct1990,  
	              vturn$college_pct, vturn$hs_pct, 
	              vturn$unemp, vturn$income, 
				  vturn$poverty, vturn$home, 
				  vturn$no_cand)					
					
# Prematch Balance		  	  
KS_out_pre <-  MatchBalance(black~ pop90 + blackpop_pct1990 + college_pct + hs_pct + 
							   unemp + income + poverty + home + no_cand, 
							   ks=TRUE, nboots=1000, data=vturn)

out_pre <- matrix(NA, 9,3)

for (i in 1:9){
	out_pre[i,1] <- unlist((KS_out_pre$BeforeMatching)[[i]])[3]
  	out_pre[i,2] <- unlist((KS_out_pre$BeforeMatching)[[i]])[4]
  	out_pre[i,3] <- unlist((KS_out_pre$BeforeMatching)[[i]])[11]

}
out_pre

# Convert Proportions to Percentages
out_pre[2,1] <-  out_pre[2,1]*100
out_pre[3,1] <-  out_pre[3,1]*100
out_pre[4,1] <-  out_pre[4,1]*100
out_pre[5,1] <-  out_pre[5,1]*100
out_pre[7,1] <-  out_pre[7,1]*100
out_pre[8,1] <-  out_pre[8,1]*100

out_pre[2,2] <-  out_pre[2,2]*100
out_pre[3,2] <-  out_pre[3,2]*100
out_pre[4,2] <-  out_pre[4,2]*100
out_pre[5,2] <-  out_pre[5,2]*100
out_pre[7,2] <-  out_pre[7,2]*100
out_pre[8,2] <-  out_pre[8,2]*100

out_pre

#Create Rank-based Mahalanobis Distance Matrix
dist_mat = distmat(t_ind, X_mat)

# Number of matches
	n_matches=1
	
# Moment covariates
mom_covs = cbind(vturn$blackpop_pct1990, vturn$pop90,   
			     vturn$college_pct, vturn$hs_pct, 
			     vturn$unemp, vturn$income, vturn$poverty, 
			     vturn$home) 
			     
mom_weights = NULL
mom_tols = c(.02, 5000, .05, .05, .05, 5000, .05, .05)
 	
# Kolmogorov-Smirnov covariates 
ks_covs = cbind(vturn$blackpop_pct1990, vturn$pop90) 
ks_n_grid = 10
ks_weights = NULL
ks_tols = c(.02, 5000)
 
# Covariates for near-exact matching, fine and near-fine balance  Sexact_covs = NULL
exact_covs = NULL
near_exact_covs = cbind(vturn$year)
near_exact_devs = 8
fine_covs = NULL
near_fine_covs = NULL
near_fine_devs = NULL
 	
 	
# Whether specific controls need to be used
use_controls = NULL
 	
# Enforce all the constraints
enforce_constraints = FALSE
	
# Whether all the treated units need to be used
	subset_weight = 1
	
out.2 = subsetmatch(dist_mat, t_ind, n_matches, 
					  mom_covs, mom_weights, mom_tols,
					  exact_covs, 
					  near_exact_covs, near_exact_devs, 
					  fine_covs, 
					  near_fine_covs, near_fine_devs, 
					  subset_weight,
					  use_controls,
					  enforce_constraints)

#Compare Number of Treated Obs Before and After Matching
table(t_ind)
c(length(out.2$t_id), length(out.2$c_id))
	
data.match.runoff <- vturn[c(out.2$t_id, out.2$c_id),]
data.match.runoff$pair.id <- out.2$group_id
head(data.match.runoff)
tail(data.match.runoff)

# Describe covariate balance 	
KS_out_post <-  MatchBalance(black~ pop90 + blackpop_pct1990 + college_pct + hs_pct + 
							   unemp + income + poverty + home + no_cand, 
							   ks=TRUE, nboots=1000, data=data.match.runoff)

out_post <- matrix(NA, 9,3)

for (i in 1:9){
	out_post[i,1] <- unlist((KS_out_post$BeforeMatching)[[i]])[3]
  	out_post[i,2] <- unlist((KS_out_post$BeforeMatching)[[i]])[4]
  	out_post[i,3] <- unlist((KS_out_post$BeforeMatching)[[i]])[11]

}

out_post
# Convert Proportions to Percentages
out_post[2,1] <-  out_post[2,1]*100
out_post[3,1] <-  out_post[3,1]*100
out_post[4,1] <-  out_post[4,1]*100
out_post[5,1] <-  out_post[5,1]*100
out_post[7,1] <-  out_post[7,1]*100
out_post[8,1] <-  out_post[8,1]*100

out_post[2,2] <-  out_post[2,2]*100
out_post[3,2] <-  out_post[3,2]*100
out_post[4,2] <-  out_post[4,2]*100
out_post[5,2] <-  out_post[5,2]*100
out_post[7,2] <-  out_post[7,2]*100
out_post[8,2] <-  out_post[8,2]*100

out_post
names <- c("Municipal Population", "African American (%)", "College Degree (%)", 
			"High School (%)", "Unemployed (%)", "Median Income", "Below Poverty Line (%)",
			"Home Rule (0/1)", "# First Round Candidates")
out <- cbind(out_pre, out_post)			
rownames(out) <- names
out
xtable(out, digits=c(1,1,1,2,1,1,2))

t_id = out.2$t_id	
c_id = out.2$c_id
xtable(finetab(vturn$year, t_id, c_id))

setwd("~/Documents/Black Mayors/Replication File/Matches")
save(data.match.runoff, file="MatchRunOff_Gen.RData")

# Balance Stats for Data Not In Match
data.notmatch <- vturn[c(-out.2$t_id, -out.2$c_id),]

MatchBalance(black~ pop90 + blackpop_pct1990 + college_pct + hs_pct + 
							   unemp + income + poverty + home + no_cand, 
							   ks=TRUE, nboots=1000, data=data.match.runoff)

KS_notmatch <-  MatchBalance(black~ pop90 + blackpop_pct1990 + college_pct + hs_pct + 
							   unemp + income + poverty + home + no_cand, 
							   ks=TRUE, nboots=1000, data=data.notmatch)
out_notmatch <- matrix(NA, 9, 2)

for (i in 1:9){
	out_notmatch[i,1] <- unlist((KS_notmatch $BeforeMatching)[[i]])[3]
  	out_notmatch[i,2] <- unlist((KS_notmatch $BeforeMatching)[[i]])[4]  	
}

out_notmatch
out_notmatch[2,1] <-  out_notmatch[2,1]*100
out_notmatch[3,1] <-  out_notmatch[3,1]*100
out_notmatch[4,1] <-  out_notmatch[4,1]*100
out_notmatch[5,1] <-  out_notmatch[5,1]*100
out_notmatch[7,1] <-  out_notmatch[7,1]*100
out_notmatch[8,1] <-  out_notmatch[8,1]*100

out_notmatch[2,2] <-  out_notmatch[2,2]*100
out_notmatch[3,2] <-  out_notmatch[3,2]*100
out_notmatch[4,2] <-  out_notmatch[4,2]*100
out_notmatch[5,2] <-  out_notmatch[5,2]*100
out_notmatch[7,2] <-  out_notmatch[7,2]*100
out_notmatch[8,2] <-  out_notmatch[8,2]*100
	
tabout <- cbind(out_notmatch, out_post[,1:2])

rownames(tabout) <- names
print(xtable(tabout, digits=1), include.rownames=TRUE)

